<?php

class CommentsWidget extends CWidget
{
    public $oRow = array();

	private $_html = '';

	private $_oComments = array();

    public function init()
    {
	    $oComments = new Comments();
	    $this->_oComments = $oComments->getByTargetId($this->oRow->id);
//	    print_r($this->_oComments); exit;
    }

    public function run()
    {
	    $this->_html = '<div class="comments" id="review" itemprop="review" itemscope itemtype="http://schema.org/Review"><div class="title">'. _('Отзывы') .'</div>';
        $this->_tree($this->_oComments);

	    if(!empty($_COOKIE['comment_success']) && $_COOKIE['comment_success'])
	    {
		    $this->_html .= '<div class="review-success">Спасибо за ваш ценный отзыв.</div>';

		    Yii::app()->request->cookies['comment_success'] = new CHttpCookie('comment_success', 0, array(
		                                                                                                 'httpOnly' => true,
		                                                                                                 'expire' => time() - 1,
		                                                                                                 'path' => '/'
		                                                                                            ));
	    }
	    else
	    {
		    $this->_html .= '
		        '. (Messages::getInst()->getMessages() ? '<div class="message">'. Messages::getInst()->getMessage('error') .'</div>' : '') .'
			    <form id="review-form" action="'. Yii::app()->createUrl('/comments/add', array()) .'" method="post">
			        <input type="hidden" name="target_id" value="'. $this->oRow->id .'" />
					<div class="fitem">
						<input type="text" name="user_name" placeholder="Имя" required />
					</div>
					<div class="fitem">
						<input type="email" name="email" placeholder="your.email@example.com" />
					</div>
					<div class="fitem">
						<textarea name="comment" placeholder="Ваш отзыв о товаре" required>'. (!empty($_COOKIE['comment']) ? $_COOKIE['comment'] : '') .'</textarea>
					</div>
					<div class="fitem">
						<button type="submit"><span>'. _('Отправить') .'</span></button>
					</div>
			    </form>
		    </div>
		    ';
	    }
    }

	private function _tree($oRowset, $level = 1)
	{
		if(empty($oRowset))
			return false;

		foreach($oRowset as $oRow)
		{
			$this->_html .= '
			<div class="com-item level-'. $level .'">
				<b><a href="" itemprop="author">'. $oRow->user_name .'</a><meta itemprop="datePublished" content="'. date('Y-m-d', strtotime($oRow->date_add)) .'" /></b>
				<div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
					<meta itemprop="worstRating" content="1"/>
					<meta itemprop="bestRating" content="5"/>
				    <span itemprop="ratingValue">4</span>
			    </div>
				<p itemprop="description">'. $oRow->comment .'</p>';

			if(!empty($oRow->childNodes))
				$this->_tree($oRow->childNodes, $level+1);
			$this->_html .= '</div>';
		}

		return true;
	}

    public function __toString()
    {
        return $this->_html;
    }
}
